package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.SystemUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys_user")
public class SystemUserController {
    @Reference
    private SystemUserService systemUserService;

    /**
     * 分页查询用户
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        try {
            PageResult pageResult = systemUserService.pageQuery(queryPageBean);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }
    /**
     * 查询所有角色数据
     *
     * @return
     */
    @GetMapping(path = "/findAll")
    public Result findAll() {
        try {
            List<Role> roles = systemUserService.findAll();
            return new Result(true, MessageConstant.QUERY_Role_SUCCESS, roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_Role_FAIL);
        }
    }
    /**
     * 新增用户
     * @param user
     * @return
     */
    @RequestMapping(path = "/add")
    public Result add(@RequestBody User user,Integer[] roleIds){
        try {
            systemUserService.add(user,roleIds);
                return new Result(true,MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
    }

    /**
     * 根据id查询角色信息详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        try {
            User user = systemUserService.findById(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

    /**
     * 查询指定用户关联的角色id
     * @param userId
     * @return
     */
    @GetMapping("/{id}/roles")
    public Result findRoleAssociationIds(@PathVariable("id") Integer userId){
        try {
            Integer[] roleAssociationIds = systemUserService.findRoleAssociationIds(userId);
            return new Result(true,MessageConstant.QUERY_Role_SUCCESS,roleAssociationIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_Role_FAIL);
        }
    }

    /**
     * 根据角色Id更新用户基本信息
     * @param user
     * @param roleIds
     * @return
     */
    @PutMapping("/updata")
    public Result update(@RequestBody User user, Integer[] roleIds) {
        try {
            systemUserService.update(user, roleIds);
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_USER_FAIL);
        }
    }

    /**
     * 根据ID删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        try {
            systemUserService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }
}
